package com.auoem.radio.common.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

import com.auoem.radio.base.OemApplication;

/**
 * 字符串的操作
 *
 * @author 胡荣华
 */
public class ResourceUtils {

    /**
     * 取指定资源颜色对像值
     *
     * @param ColorId
     * @return
     */
    public static int getResourcesColor(int ColorId) {
        return getResourcesColor(ColorId, 0xFFFFFFFF);
    }

    private static Context getAppContext() {
        return OemApplication.getApplication();
    }

    /**
     * 取指定资源颜色对像值
     *
     * @param ColorId      颜色ID
     * @param DefaultColor
     * @return
     */
    public static int getResourcesColor(int ColorId, int DefaultColor) {
        if (ColorId == -1)
            return DefaultColor;
        //todo 移植来部分，不需判断，后续去除
        if (getAppContext() != null) {
            try {
                return getAppContext().getResources().getColor(ColorId);
            } catch (Resources.NotFoundException mx) {
            }
        }
        return DefaultColor;
    }

    /**
     * 取指定资源字符串对像值
     *
     * @param StringID
     * @return
     */
    public static String getResourcesString(int StringID) {
        if (StringID == -1)
            return "";
        if (getAppContext() != null) {
            try {
                return getAppContext().getResources().getString(StringID);
            } catch (Resources.NotFoundException mx) {
            }
        }
        return "";
    }

    /**
     * 取指定资源字符串对像值(例如 Hi,%1$s你好吗?)
     *
     * @param StringID
     * @param obj
     * @return
     */
    public static String getResourcesString(int StringID, Object... obj) {
        return String.format(getResourcesString(StringID), obj);
    }

    /**
     * 取指定资源尺寸对象值
     *
     * @param StringID
     * @return
     */
    public static float getResourcesDimension(int StringID) {
        if (StringID == -1)
            return 0;
        if (getAppContext() != null) {
            try {
                return getAppContext().getResources().getDimension(StringID);
            } catch (Resources.NotFoundException mx) {
            }
        }
        return 0;
    }

    /**
     * 取指定资源尺寸对象值
     *
     * @param drawableId
     * @return
     */
    public static Drawable getResourcesDrawable(int drawableId) {
        if (drawableId == -1)
            return null;
        if (getAppContext() != null) {
            try {
                return getAppContext().getResources().getDrawable(drawableId);
            } catch (Resources.NotFoundException mx) {
            }
        }
        return null;
    }

    public static String[] getStringArrays(int arrayId) {
        if (arrayId == -1)
            return null;
        if (getAppContext() != null) {
            try {
                return getAppContext().getResources().getStringArray(arrayId);
            } catch (Resources.NotFoundException mx) {
            }
        }
        return null;
    }
}
